import sys
sys.path.insert(0,'../..')

from rmpoly import *
from gmpy import mpq,version
from time import time

print 'using gmpy', version()

def test_reversion2a():
  rp = RPoly(['z','y','x'],10,mpq)
  z,y,x = rp.gens()
  nx = 12
  ny = 12
  p = x*(3+y+y**2)
  for i in range(2,nx):
    p += x**i*(1+y)**i
  t0 = time()
  p1 = p.series_reversion2('x',nx,'y',ny,'z')
  t1 = time()
  print 'reversion %.2f number of terms=%d' %(t1-t0, len(p1))
  sb = Subs(rp,rp,{'x':p1})
  p2 = sb.subs_trunc(p,['z','y'],[nx,ny])
  assert p2 == z

def test_reversion2b():
  rp = RPoly(['w','z','y','x'],10,mpq)
  w,z,y,x = rp.gens()
  p = x*(2 + y**2 + z) + x**2*(1+y+z) + x**3*(5+y*2+z)
  n = nx=ny=nz=7
  for i in range(2,n):
    p += x**i*(1+y+z)**i
  t0 = time()
  p1 = p.series_reversion2('x',nx,['y','z'],[ny,nz],'w')
  t1 = time()
  print 'reversion %.2f number of terms=%d' %(t1-t0, len(p1))
  sb = Subs(rp,rp,{'x':p1})
  p2 = sb.subs_trunc(p,['w','y','z'],[nx,ny,nz])
  assert p2 == w

test_reversion2a()
test_reversion2b()
